package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.api.service.AgentServiceRpc;
import com.weixing.mall.provider.api.service.ItemFeign;
import com.weixing.mall.provider.api.service.SysUserRpcService;
import com.weixing.mall.provider.mapper.OrderGoodsMapper;
import com.weixing.mall.provider.mapper.OrderMapper;
import com.weixing.mall.provider.model.enums.ProductStatusEnum;
import com.weixing.mall.provider.model.query.MarketQuery;
import com.weixing.mall.provider.model.vo.MarketReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.*;

@Service
public class MarketServiceImpl {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderGoodsMapper orderGoodsMapper;
    @Autowired
    private ItemFeign itemFeign;
    @Autowired
    private SysUserRpcService sysUserRpcService;

    @Autowired
    private AgentServiceRpc agentServiceRpc;

    public  IPage<MarketReportVo> marketIndex(Page page, MarketQuery marketQuery){
        Long [] agentIds=getAentIds(marketQuery);
        IPage<MarketReportVo> result=page;
        List<MarketReportVo>  tmpList=  orderGoodsMapper.marketIndex(marketQuery,agentIds);
        result.setRecords( dataIntegration(tmpList));
        result.setTotal(tmpList.size());
        return  result;
    }

    private Long[] getAentIds(MarketQuery marketQuery) {
        if(null!=marketQuery.getAgentId()){
            Long [] agentIds=agentServiceRpc.selectAgentTreeByParentId(marketQuery.getAgentId());
            marketQuery.setAgentIds(agentIds);
            return agentIds;
        }
        return null;
    }

    public  MarketReportVo  MarketIndexBottom ( MarketQuery marketQuery){
        Long [] agentIds=getAentIds(marketQuery);
        List<MarketReportVo>  tmpList=  orderGoodsMapper.marketIndex(marketQuery,agentIds);
        MarketReportVo  result=new MarketReportVo();
        BigDecimal totalOutStock=new BigDecimal(0);
        BigDecimal totalReturn=new BigDecimal(0);
        BigDecimal totalReturnlAmout=new BigDecimal(0);
        BigDecimal totalCanel=new BigDecimal(0);
        BigDecimal totalSales=new BigDecimal(0);
        BigDecimal totalSalesAmount=new BigDecimal(0);
        for( MarketReportVo comparVo:tmpList){
            if(null!=comparVo.getProductStatus()) {
                if (comparVo.getProductStatus().equals(ProductStatusEnum.STATUS6.getValue())) {//断货
                    totalOutStock=   totalOutStock.add(comparVo.getProductStatusCount());

                }
                if (comparVo.getProductStatus().equals(ProductStatusEnum.STATUS7.getValue())) {//取消
                    totalCanel=   totalCanel.add(comparVo.getProductStatusCount());

                }
                if (comparVo.getProductStatus().equals(ProductStatusEnum.STATUS9.getValue())) {//退货
                    totalReturn=   totalReturn.add(comparVo.getProductStatusCount());
                    totalReturnlAmout= totalReturnlAmout.add(comparVo.getSumprice());
                }
            }
            totalSales=  totalSales.add(comparVo.getProductStatusCount());
            totalSalesAmount= totalSalesAmount.add(comparVo.getSumprice());
        }
        result.setTotalSalesAmount(totalSalesAmount);
        result.setTotalSales(totalSales);
        result.setTotalReturn(totalReturn);
        result.setTotalReturnlAmout(totalReturnlAmout);
        result.setTotalCanel(totalCanel);
        result.setTotalOutStock(totalOutStock);
        return result;
    }
    /*
    * 对首页的数据进行整合
    * */
    public List<MarketReportVo>   dataIntegration(List<MarketReportVo> voList){
        List<MarketReportVo> result=Lists.newArrayList();
        boolean flag=true;//数据控制开关
        for( MarketReportVo vo:voList) {
            for (MarketReportVo checkVo : result) {
                if (vo.getCreatedTime().equals(checkVo.getCreatedTime())) {
                    flag = false;
                }
            }
            if (flag) {
                BigDecimal totalSales=new BigDecimal(0);
                BigDecimal totalSalesAmount=new BigDecimal(0);
                for (MarketReportVo comparVo : voList) {
                    if (vo.getCreatedTime().equals(comparVo.getCreatedTime())) {
                        if(null!=comparVo.getProductStatus()) {
                            if (comparVo.getProductStatus().equals(ProductStatusEnum.STATUS6.getValue())) {//断货
                                vo.setTotalOutStock(comparVo.getProductStatusCount());
                            }
                            if (comparVo.getProductStatus().equals(ProductStatusEnum.STATUS7.getValue())) {//取消
                                vo.setTotalCanel(comparVo.getProductStatusCount());
                            }
                            if (comparVo.getProductStatus().equals(ProductStatusEnum.STATUS9.getValue())) {//退货
                                vo.setTotalReturn(comparVo.getProductStatusCount());
                                vo.setTotalReturnlAmout(comparVo.getSumprice());
                            }
                        }
                        totalSales=totalSales.add(comparVo.getProductStatusCount());
                        totalSalesAmount=totalSalesAmount.add(comparVo.getSumprice());
                    }
                    vo.setTotalSales(totalSales);
                    vo.setTotalSalesAmount(totalSalesAmount);
                }
            }
            if(flag){
                result.add(vo);
            }
        }
        return  voList;
    }

}
